@model EIMS.Models.ViewModels.SupplierDetailViewModel

@{
    ViewData["Title"] = "供应商详情";
}

<div class="container-fluid px-4">
    <h1 class="mt-4">@ViewData["Title"]</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li class="breadcrumb-item"><a asp-controller="Supplier" asp-action="Index">供应商管理</a></li>
        <li class="breadcrumb-item active">@ViewData["Title"]</li>
    </ol>
    
    @if (TempData["SuccessMessage"] != null)
    {
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-1"></i> @TempData["SuccessMessage"]
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    }

    <div class="mb-3">
        <a asp-action="Index" class="btn btn-outline-secondary">返回列表</a>
        @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
        {
            <a asp-action="Edit" asp-route-id="@Model.Supplier?.Id" class="btn btn-outline-primary ms-1">编辑</a>
            @if (User.IsInRole("Administrator"))
            {
                <a asp-action="Delete" asp-route-id="@Model.Supplier?.Id" class="btn btn-outline-danger ms-1">删除</a>
            }
        }
    </div>

    <div class="row">
        <div class="col-xl-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="fas fa-id-card me-1"></i>
                    基本信息
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.SupplierNumber)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.SupplierNumber)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Name)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.Name)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Type)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.Type)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.CreditRating)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.CreditRating)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Status)
                        </div>
                        <div class="col-sm-8">
                            <span class="badge bg-@(Model.Supplier?.Status == EIMS.Models.Enums.SupplierStatus.Active ? "success" : 
                                               Model.Supplier?.Status == EIMS.Models.Enums.SupplierStatus.Suspended ? "warning" : "danger")">
                                @Html.DisplayFor(model => model.Supplier!.Status)
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="fas fa-address-book me-1"></i>
                    联系信息
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.ContactPerson)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.ContactPerson)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.PhoneNumber)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.PhoneNumber)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Email)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.Email)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Address)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.Address)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Website)
                        </div>
                        <div class="col-sm-8">
                            @if (!string.IsNullOrEmpty(Model.Supplier?.Website))
                            {
                                <a href="@Model.Supplier!.Website" target="_blank">@Model.Supplier.Website</a>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="fas fa-university me-1"></i>
                    财务信息
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.BankAccount)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.BankAccount)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.BankName)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.BankName)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.TaxNumber)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.TaxNumber)
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-6">
            <div class="card mb-4">
                <div class="card-header">
                    <i class="fas fa-info-circle me-1"></i>
                    其他信息
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.Remarks)
                        </div>
                        <div class="col-sm-8">
                            @Html.DisplayFor(model => model.Supplier!.Remarks)
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.CreatedAt)
                        </div>
                        <div class="col-sm-8">
                            @(Model.Supplier?.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss"))
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-sm-4 text-muted">
                            @Html.DisplayNameFor(model => model.Supplier!.UpdatedAt)
                        </div>
                        <div class="col-sm-8">
                            @(Model.Supplier?.UpdatedAt.HasValue == true ? Model.Supplier.UpdatedAt.Value.ToString("yyyy-MM-dd HH:mm:ss") : "-")
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-12">
            <div class="card mb-4">
                <div class="card-header">
                    <ul class="nav nav-tabs card-header-tabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="contacts-tab" data-bs-toggle="tab" data-bs-target="#contacts" type="button" role="tab" aria-controls="contacts" aria-selected="true">联系人</button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="transactions-tab" data-bs-toggle="tab" data-bs-target="#transactions" type="button" role="tab" aria-controls="transactions" aria-selected="false">交易记录</button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="summary-tab" data-bs-toggle="tab" data-bs-target="#summary" type="button" role="tab" aria-controls="summary" aria-selected="false">统计</button>
                        </li>
                    </ul>
                </div>
                <div class="card-body">
                    <div class="tab-content">
                        <div class="tab-pane fade show active" id="contacts" role="tabpanel" aria-labelledby="contacts-tab">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="card-title">联系人列表</h5>
                                @if ((User.IsInRole("Administrator") || User.IsInRole("Manager")) && Model.Supplier != null)
                                {
                                    <a asp-action="AddContact" asp-route-id="@Model.Supplier?.Id" class="btn btn-sm btn-primary">
                                        <i class="fas fa-plus"></i> 添加联系人
                                    </a>
                                }
                            </div>
                            
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>姓名</th>
                                            <th>职位</th>
                                            <th>电话</th>
                                            <th>邮箱</th>
                                            <th>默认联系人</th>
                                            <th class="text-center">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    @if (Model.Contacts != null && Model.Contacts.Any())
                                    {
                                        foreach (var contact in Model.Contacts)
                                        {
                                            <tr>
                                                <td>@Html.DisplayFor(modelItem => contact.Name)</td>
                                                <td>@Html.DisplayFor(modelItem => contact.Position)</td>
                                                <td>@Html.DisplayFor(modelItem => contact.Mobile)</td>
                                                <td>@Html.DisplayFor(modelItem => contact.Email)</td>
                                                <td>
                                                    @if (contact.IsDefault)
                                                    {
                                                        <span class="badge bg-primary">默认</span>
                                                    }
                                                </td>
                                                <td class="text-center">
                                                    <div class="btn-group">
                                                        @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                                                        {
                                                            <a asp-action="EditContact" asp-route-id="@contact.Id" class="btn btn-sm btn-primary">
                                                                <i class="fas fa-edit"></i>
                                                            </a>
                                                            <a asp-action="DeleteContact" asp-route-id="@contact.Id" class="btn btn-sm btn-danger">
                                                                <i class="fas fa-trash"></i>
                                                            </a>
                                                        }
                                                    </div>
                                                </td>
                                            </tr>
                                        }
                                    }
                                    else
                                    {
                                        <tr>
                                            <td colspan="6" class="text-center">暂无联系人</td>
                                        </tr>
                                    }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        
                        <div class="tab-pane fade" id="transactions" role="tabpanel" aria-labelledby="transactions-tab">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="card-title">交易记录</h5>
                                @if ((User.IsInRole("Administrator") || User.IsInRole("Manager")) && Model.Supplier != null)
                                {
                                    <a asp-action="AddTransaction" asp-route-id="@Model.Supplier?.Id" class="btn btn-sm btn-primary">
                                        <i class="fas fa-plus"></i> 添加交易记录
                                    </a>
                                }
                            </div>
                            
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>交易编号</th>
                                            <th>交易日期</th>
                                            <th>金额</th>
                                            <th>交易类型</th>
                                            <th>支付方式</th>
                                            <th>支付状态</th>
                                            <th class="text-center">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    @if (Model.Transactions != null && Model.Transactions.Any())
                                    {
                                        foreach (var transaction in Model.Transactions)
                                        {
                                            <tr>
                                                <td>@Html.DisplayFor(modelItem => transaction.TransactionNumber)</td>
                                                <td>@transaction.TransactionDate.ToString("yyyy-MM-dd")</td>
                                                <td>@Html.DisplayFor(modelItem => transaction.Amount)</td>
                                                <td>@Html.DisplayFor(modelItem => transaction.TransactionType)</td>
                                                <td>@Html.DisplayFor(modelItem => transaction.PaymentMethod)</td>
                                                <td>
                                                    <span class="badge bg-@(transaction.PaymentStatus == EIMS.Models.Enums.PaymentStatus.Paid ? "success" : 
                                                                         transaction.PaymentStatus == EIMS.Models.Enums.PaymentStatus.PartiallyPaid ? "warning" : "danger")">
                                                        @Html.DisplayFor(modelItem => transaction.PaymentStatus)
                                                    </span>
                                                </td>
                                                <td class="text-center">
                                                    <div class="btn-group">
                                                        @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                                                        {
                                                            <a asp-action="EditTransaction" asp-route-id="@transaction.Id" class="btn btn-sm btn-primary">
                                                                <i class="fas fa-edit"></i>
                                                            </a>
                                                            <a asp-action="DeleteTransaction" asp-route-id="@transaction.Id" class="btn btn-sm btn-danger">
                                                                <i class="fas fa-trash"></i>
                                                            </a>
                                                        }
                                                    </div>
                                                </td>
                                            </tr>
                                        }
                                    }
                                    else
                                    {
                                        <tr>
                                            <td colspan="7" class="text-center">暂无交易记录</td>
                                        </tr>
                                    }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        
                        <div class="tab-pane fade" id="summary" role="tabpanel" aria-labelledby="summary-tab">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="card bg-primary text-white mb-4">
                                        <div class="card-body">
                                            <h5 class="card-title">交易次数</h5>
                                            <h2 class="display-4">@Model.TransactionCount</h2>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-success text-white mb-4">
                                        <div class="card-body">
                                            <h5 class="card-title">交易总金额</h5>
                                            <h2 class="display-4">¥ @Model.TotalTransactionAmount</h2>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-warning text-white mb-4">
                                        <div class="card-body">
                                            <h5 class="card-title">未付款金额</h5>
                                            <h2 class="display-4">¥ @Model.UnpaidAmount</h2>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>